const page_init = async () => {
    $("body>.page>.main>article>section>p").click(pCopy);
};

// P标签的点击复制功能
const pCopy = async ({currentTarget: DOM}) => {
    const value = $.trim($(DOM).clone().children().remove().end().text()) || $(DOM).find("span").eq(0).text();
    await copyText(value);
    vhToast();
};

let toast_timer = null;
const vhToast = (msg = "复制成功") => {
    clearTimeout(toast_timer);
    $(".ξvh-toast").text(msg).addClass("active");
    toast_timer = setTimeout(() => {
        $(".ξvh-toast").removeClass("active");
    }, 1666);
};

// 复制功能
const copyText = async text => {
    try {
        await navigator.clipboard.writeText(text);
    } catch (err) {
        await new Promise(r => {
            const textArea = document.createElement("textarea");
            textArea.setAttribute("readonly", "readonly");
            textArea.style["opacity"] = "0";
            textArea.style["pointer-events"] = "none";
            textArea.style["position"] = "absolute";
            textArea.value = text;
            document.body.appendChild(textArea);
            textArea.select();
            document.execCommand("copy");
            document.body.removeChild(textArea);
            r(true);
        });
    }
};

$(function () {
    page_init();
});
